package com.jcroke.PC32.basetransform;

import java.util.ArrayList;
import java.util.Iterator;

import com.jcroke.DACGraph.Pnode;
import com.jcroke.PC32.memory.Umem;
import com.jcroke.PC32.serialize.Record;

/**
 * 
 * @author jcroke
 *
 */
public class FunctionalContainer {
	private Record _record;
	private ArrayList<Pnode> _PnList;
	private ArrayList<Functional> _TFSList;
	private Iterator<Pnode> _pIt = null;
	private Iterator<Functional> _fIt = null;

	public FunctionalContainer(Record record) {
		this._record = record;
		this._PnList = new ArrayList<Pnode>();
		this._TFSList = new ArrayList<Functional>();
	}

	public void add(Pnode p, Functional f) {
		this._PnList.add(p);
		this._TFSList.add(f);
	}

	public boolean hasFunctional() {
		return (_fIt != null && _fIt.hasNext() && _pIt != null & _pIt.hasNext());
	}

	public Functional getFunctional() {
		// lock step functional and p-node lookups
		Functional tRet = null;
		if (_fIt != null && _pIt != null) {
			tRet = _fIt.next();
			tRet.prepare(_pIt.next());
		}
		return (tRet);
	}

	public void reset() {
		// create new iterators.
		this._fIt = _TFSList.iterator();
		this._pIt = _PnList.iterator();
		
		// clear memory
		_record.getUmem().clear();
	}
	
	public Umem getMemoryObject() {
		return _record.getUmem();
	}
}
